package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.FeeKey;
import com.atguigu.lease.model.entity.FeeValue;
import com.atguigu.lease.web.admin.mapper.FeeKeyMapper;
import com.atguigu.lease.web.admin.service.FeeKeyService;
import com.atguigu.lease.web.admin.service.FeeValueService;
import com.atguigu.lease.web.admin.vo.fee.FeeKeyVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.PrinterURI;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author liubo
* @description 针对表【fee_key(杂项费用名称表)】的数据库操作Service实现
* @createDate 2023-07-24 15:48:00
*/
@Service
public class FeeKeyServiceImpl extends ServiceImpl<FeeKeyMapper, FeeKey>
    implements FeeKeyService{
    @Autowired
    private FeeValueService feeValueService;

    @Override
    public List<FeeKeyVo> getFeeInfoList() {
        List<FeeKey> feeKeys = this.list();
        List<Long> feeKeyIds = feeKeys.stream().map(FeeKey::getId).collect(Collectors.toList());
        List<FeeValue> feeValues = feeValueService.list(Wrappers.lambdaQuery(FeeValue.class).in(FeeValue::getFeeKeyId, feeKeyIds));
        Map<Long, List<FeeValue>> feeValueMap = feeValues.stream().collect(Collectors.groupingBy(FeeValue::getFeeKeyId));

        return feeKeys.stream().map(item -> {
            FeeKeyVo feeKeyVo = new FeeKeyVo();
            BeanUtils.copyProperties(item,feeKeyVo);
            feeKeyVo.setFeeValueList(feeValueMap.get(item.getId()));
            return feeKeyVo;
        }).collect(Collectors.toList());
    }
}




